package com.ruoyi.zhjd.laboratory.modules.disconnectRecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhjd.laboratory.modules.disconnectRecord.domain.NSiteDisconnectRecord;
import com.ruoyi.zhjd.laboratory.modules.disconnectRecord.domain.bo.NSiteDisconnectRecordBo;
import com.ruoyi.zhjd.laboratory.modules.disconnectRecord.domain.vo.NSiteDisconnectRecordVo;
import com.ruoyi.zhjd.laboratory.modules.disconnectRecord.mapper.NSiteDisconnectRecordMapper;
import com.ruoyi.zhjd.laboratory.modules.disconnectRecord.service.INSiteDisconnectRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工地现场断网记录Service业务层处理
 *
 * @author zyb
 * @date 2023-10-13
 */
@Service
@DS("sjcj")
public class NSiteDisconnectRecordServiceImpl implements INSiteDisconnectRecordService {

	@Autowired
    private NSiteDisconnectRecordMapper baseMapper;



    /**
     * 查询实验室断网记录
     */
    @Override
    public TableDataInfo<NSiteDisconnectRecordVo> queryById(String type, PageQuery pageQuery){
    	Page<NSiteDisconnectRecordVo> result = baseMapper.selectHistoryById(pageQuery.build(), type);
        return TableDataInfo.build(result);
    }
    /**
     * 查询实验室实时断网记录记录
     */
    @Override
	public TableDataInfo<NSiteDisconnectRecordVo> getSiteOnlineRecord(NSiteDisconnectRecordBo bo, PageQuery pageQuery) {
    	Page<NSiteDisconnectRecordVo> result = baseMapper.getSiteOnlineRecord(pageQuery.build(), bo);
        return TableDataInfo.build(result);
	}
    /**
     * 查询历史记录列表
     */
    @Override
	public TableDataInfo<NSiteDisconnectRecordVo> equipmentOffline(NSiteDisconnectRecordBo bo, PageQuery pageQuery) {
    	 Page<NSiteDisconnectRecordVo> result = baseMapper.equipmentOffline(pageQuery.build(), bo);
         return TableDataInfo.build(result);
	}
    /**
     * 查询历史记录详情列表
     */
    @Override
	public TableDataInfo<NSiteDisconnectRecordVo> equipmentOfflineDetail(NSiteDisconnectRecordBo bo,
			PageQuery pageQuery) {
    	Page<NSiteDisconnectRecordVo> result = baseMapper.equipmentOfflineDetail(pageQuery.build(), bo);
        return TableDataInfo.build(result);
	}
    /**
     * 查询工地现场断网记录列表
     */
    @Override
    public TableDataInfo<NSiteDisconnectRecordVo> queryPageList(NSiteDisconnectRecordBo bo, PageQuery pageQuery) {
        Page<NSiteDisconnectRecordVo> result = baseMapper.page(pageQuery.build(), bo);
        //List<NSiteDisconnectRecordVo> records = result.getRecords();
        return TableDataInfo.build(result);
    }
    @Override
	public TableDataInfo<NSiteDisconnectRecordVo> queryBhzPageList(NSiteDisconnectRecordBo bo, PageQuery pageQuery) {
    	 Page<NSiteDisconnectRecordVo> result = baseMapper.pageBhz(pageQuery.build(), bo);
         //List<NSiteDisconnectRecordVo> records = result.getRecords();
         return TableDataInfo.build(result);
	}
    /**
     * 查询工地现场断网记录列表
     */
    @Override
    public List<NSiteDisconnectRecordVo> queryList(NSiteDisconnectRecordBo bo) {
        LambdaQueryWrapper<NSiteDisconnectRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NSiteDisconnectRecord> buildQueryWrapper(NSiteDisconnectRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NSiteDisconnectRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NSiteDisconnectRecord::getType, bo.getType());
        lqw.eq(bo.getSourceId() != null, NSiteDisconnectRecord::getSourceId, bo.getSourceId());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupId()), NSiteDisconnectRecord::getGroupId, bo.getGroupId());
        lqw.eq(bo.getGroupNo() != null, NSiteDisconnectRecord::getGroupNo, bo.getGroupNo());
        lqw.eq(bo.getBootTime() != null, NSiteDisconnectRecord::getBootTime, bo.getBootTime());
        lqw.eq(bo.getDisconnectBegin() != null, NSiteDisconnectRecord::getDisconnectBegin, bo.getDisconnectBegin());
        lqw.eq(bo.getDisconnectEnd() != null, NSiteDisconnectRecord::getDisconnectEnd, bo.getDisconnectEnd());
        lqw.eq(bo.getDisconnectDuration() != null, NSiteDisconnectRecord::getDisconnectDuration, bo.getDisconnectDuration());
        lqw.eq(bo.getUnattendedTime() != null, NSiteDisconnectRecord::getUnattendedTime, bo.getUnattendedTime());
        lqw.eq(bo.getSend() != null, NSiteDisconnectRecord::getSend, bo.getSend());
        lqw.eq(StringUtils.isNotBlank(bo.getIsNormalShutdown()), NSiteDisconnectRecord::getIsNormalShutdown, bo.getIsNormalShutdown());
        lqw.eq(bo.getCreateDate() != null, NSiteDisconnectRecord::getCreateDate, bo.getCreateDate());
        return lqw;
    }

    /**
     * 新增工地现场断网记录
     */
    @Override
    public Boolean insertByBo(NSiteDisconnectRecordBo bo) {
        NSiteDisconnectRecord add = BeanUtil.toBean(bo, NSiteDisconnectRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工地现场断网记录
     */
    @Override
    public Boolean updateByBo(NSiteDisconnectRecordBo bo) {
        NSiteDisconnectRecord update = BeanUtil.toBean(bo, NSiteDisconnectRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NSiteDisconnectRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工地现场断网记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }





}
